package com.zyd.recyclerviewdemo.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zyd.recyclerviewdemo.R;

import java.util.List;

    public class LoadMoreAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        private List<String> dataList;

        // 普通布局
        private final int TYPE_ITEM = 1;
        private final int TYPE_HEAD = 2;
        // 脚布局
        private final int TYPE_FOOTER = 3;
        // 当前加载状态，默认为加载完成
        private int loadState = 2;
        // 正在加载
        public final int LOADING = 1;
        // 加载完成
        public final int LOADING_COMPLETE = 2;
        // 加载到底
        public final int LOADING_END = 3;

        public LoadMoreAdapter(List<String> dataList) {
            this.dataList = dataList;
        }

        @Override
        public int getItemViewType(int position) {


            // 最后一个item设置为FooterView
            if (position + 1 == getItemCount()) {
                return TYPE_FOOTER;
            } else {
                return TYPE_ITEM;
            }
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            if (viewType == TYPE_ITEM) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_load_more, parent, false);
                return new RecyclerViewHolder(view);
            } else if (viewType == TYPE_FOOTER) {
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_refresh_footer, parent, false);
                return new FootViewHolder(view);
            }
            return null;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            if (holder instanceof RecyclerViewHolder){
                RecyclerViewHolder recyclerViewHolder = (RecyclerViewHolder) holder;
                recyclerViewHolder.tvItem.setText(dataList.get(position));
            }else if (holder instanceof FootViewHolder){
                FootViewHolder footViewHolder = (FootViewHolder) holder;
                switch (loadState){
                    case LOADING:// 正在加载
                        footViewHolder.pbLoading.setVisibility(View.VISIBLE);
                        footViewHolder.tvLoading.setVisibility(View.VISIBLE);
                        footViewHolder.llEnd.setVisibility(View.GONE);
                        break;
                    case LOADING_COMPLETE: // 加载完成
                        footViewHolder.pbLoading.setVisibility(View.INVISIBLE);
                        footViewHolder.tvLoading.setVisibility(View.INVISIBLE);
                        footViewHolder.llEnd.setVisibility(View.GONE);
                        break;
                    case  LOADING_END:// 加载到底
                        footViewHolder.pbLoading.setVisibility(View.GONE);
                        footViewHolder.tvLoading.setVisibility(View.GONE);
                        footViewHolder.llEnd.setVisibility(View.VISIBLE);
                        break;
                }
            }
        }

        @Override
        public int getItemCount() {
            return dataList.size()+1;
        }


        private class RecyclerViewHolder extends RecyclerView.ViewHolder {

            TextView tvItem;

            RecyclerViewHolder(View itemView) {
                super(itemView);
                tvItem = itemView.findViewById(R.id.tv_item);
            }
        }

        @Override
        public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
            super.onAttachedToRecyclerView(recyclerView);
            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
            if (layoutManager instanceof GridLayoutManager){
                final GridLayoutManager gridLayoutManager = (GridLayoutManager)layoutManager;
                gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                    @Override
                    public int getSpanSize(int position) {
                        // 如果当前是footer的位置，那么该item占据2个单元格，正常情况下占据1个单元格
                        return getItemViewType(position) == TYPE_FOOTER ? gridLayoutManager.getSpanCount():1;
                    }
                });
            }
        }

        private class FootViewHolder extends RecyclerView.ViewHolder {
            ProgressBar pbLoading;
            TextView tvLoading;
            LinearLayout llEnd;

            public FootViewHolder(@NonNull View itemView) {
                super(itemView);
                pbLoading = itemView.findViewById(R.id.pb_loading);
                tvLoading = itemView.findViewById(R.id.tv_loading);
                llEnd = itemView.findViewById(R.id.ll_end);
            }
        }
        /**
         * 设置上拉加载状态
         *
         * @param loadState 0.正在加载 1.加载完成 2.加载到底
         */
        public void setLoadState(int loadState){
            this.loadState = loadState;
            notifyDataSetChanged();
        }
    }
